package com.czwj.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.czwj.dao.BudgetMainMapper;
import com.czwj.dao.DutyChild2Mapper;
import com.czwj.dao.DutyChild3Mapper;
import com.czwj.dao.ProjectItemChildMapper;
import com.czwj.entities.BudgetMain;
import com.czwj.entities.Duty2;
import com.czwj.entities.Duty3;
import com.czwj.entities.Duty4;
import com.czwj.entities.DutyChild2;
import com.czwj.entities.DutyChild3;
import com.czwj.entities.ProjectItem;
import com.czwj.entities.ProjectItemChild;
import com.czwj.service.ProjectItemChildService;

/**
* @ClassName: ProjectItemChildServiceImpl
* @Description: TODO
* @author H
* @date 2020年4月20日
*
*/
@Service
public class ProjectItemChildServiceImpl implements ProjectItemChildService{
	@Resource
	ProjectItemChildMapper mapper;
	
	@Resource
	DutyChild2Mapper mapper2;
	
	@Resource
	DutyChild3Mapper mapper3;
	
	@Resource
	BudgetMainMapper bmapper;
	
	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.ProjectItemChildService#search(java.util.Map)
	*/
	@Override
	public List<ProjectItemChild> search(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.search(map);
	}
	
	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.ProjectItemChildService#count(java.util.Map)
	*/
	@Override
	public int count(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.count(map);
	}
	
	/** 
	* 
	* @param item
	* @return
	* @see com.czwj.service.ProjectItemChildService#update(com.czwj.entities.ProjectItemChild)
	*/
	@Transactional
	@Override
	public int update(ProjectItemChild item){
		// TODO Auto-generated method stub
		mapper2.deleteByProjectitemid(item.getId());
		mapper3.deleteByProjectitemid(item.getId());
		int cnt = 0;
		if(item.getDuty2() != null){
			for(int d2:item.getDuty2()){
				cnt += mapper2.insertSelective(new DutyChild2(item.getId(),d2));
			}
		}
		if(item.getDuty3() != null){
			for(int d3:item.getDuty3()){
				cnt += mapper3.insertSelective(new DutyChild3(item.getId(),d3));
			}
		}
		BudgetMain budgetMain = bmapper.searchbyprojectitemchild(item.getId());
		budgetMain.setMoney(item.getCompactmoney());
		cnt += bmapper.updateByPrimaryKeySelective(budgetMain);
		cnt += this.mapper.updateByPrimaryKeySelective(item);
		return cnt;
	}
	
	/** 
	* 
	* @param item
	* @return
	* @see com.czwj.service.ProjectItemChildService#insert(com.czwj.entities.ProjectItemChild)
	*/
	@Transactional
	@Override
	public int insert(ProjectItemChild item){
		// TODO Auto-generated method stub
		int cnt = 0;
		cnt += this.mapper.insertSelective(item);
		ProjectItemChild pItem = this.mapper.findByProjectNo(item.getProjectno());
		for(int d2:item.getDuty2()){
			cnt += mapper2.insertSelective(new DutyChild2(pItem.getId(),d2));
		}
		for(int d3:item.getDuty3()){
			cnt += mapper3.insertSelective(new DutyChild3(pItem.getId(),d3));
		}
		BudgetMain budgetMain = new BudgetMain();
		budgetMain.setProjectitemid(pItem.getProjectitemid());
		budgetMain.setProjectitemchildid(pItem.getId());
		budgetMain.setStatus(2);
		budgetMain.setMoney(item.getCompactmoney());
		budgetMain.setCreatedate(new Date());
		budgetMain.setCreateip(item.getCreateip());
		budgetMain.setCreaterid(item.getCreaterid());
		cnt += bmapper.insertSelective(budgetMain);
		return cnt;
	}
	
	/** 
	* 
	* @param map
	* @return
	* @see com.czwj.service.ProjectItemChildService#updateStatus(java.util.Map)
	*/
	@Override
	public int updateStatus(Map<String,Object> map){
		// TODO Auto-generated method stub
		return this.mapper.updateStatus(map);
	}
	
	/** 
	* 
	* @param id
	* @return
	* @see com.czwj.service.ProjectItemChildService#selectByPrimaryKey(java.lang.Integer)
	*/
	@Override
	public ProjectItemChild selectByPrimaryKey(Integer id){
		// TODO Auto-generated method stub
		return this.mapper.selectByPrimaryKey(id);
	}

	/** 
	* 
	* @param id
	* @return
	* @see com.czwj.service.ProjectItemChildService#findbyfather(java.lang.Integer)
	*/
	@Override
	public List<ProjectItemChild> findbyfather(Integer id){
		// TODO Auto-generated method stub
		return this.mapper.findbyfather(id);
	}
}
